<template>
  <van-button
    :icon="value ? 'star' : 'star-o'"
    :class="{
      collected: value
    }"
  />
</template>

<script>
// 导入收藏及取消收藏文章的接口请求
import { collectArticles, cancelCollectArticles } from '@/api/users'
export default {
  name: 'CollectionArticle',
  props: {
    // 接收父组件传递服务器是否已收藏布尔值
    value: {
      type: Boolean,
      required: true
    },
    // 接收父组件传递当前文章ID
    carticleID: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async switchCollectionStatus () {
      if (this.value) {
        // 当服务器收藏状态为True，点击则取消收藏
        await cancelCollectArticles(this.carticleID)
      } else {
        // 否则为收藏该文章
        await collectArticles(this.carticleID)
      }
    }
  }
}
</script>

<style scoped lang="less">
.collected {
  /deep/.van-icon {
    color: #ffa500;
  }
}

</style>
